package org.jeecg.modules.sxaq.utils;

import org.jeecg.common.constant.CommonConstant;
import org.jeecg.common.system.api.ISysBaseAPI;
import org.jeecg.common.util.RedisUtil;
import org.jeecg.modules.sxaq.anr.entity.RecordAbnormal;
import org.jeecg.modules.sxaq.anr.service.IRecordAbnormalService;
import org.jeecg.modules.system.entity.SysUser;
import org.jeecg.modules.system.service.ISysUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.redis.connection.Message;
import org.springframework.data.redis.listener.KeyExpirationEventMessageListener;
import org.springframework.data.redis.listener.RedisMessageListenerContainer;
import org.springframework.stereotype.Component;

/**
 * @Description: REDIS工具类
 * @Author: DHC
 * @Date:  2020-12-01
 */
@Component
public class RedisKeyExpirationListener extends KeyExpirationEventMessageListener {

    @Autowired
    private ISysUserService sysUserService;
    @Autowired
    private IRecordAbnormalService recordAbnormalService;
    @Autowired
    private RedisUtil redisUtil;
    @Autowired
    private ISysBaseAPI sysBaseAPI;

    @Value("${sxaq.redisUploadTime}")
    private String redisUploadTime;

    public RedisKeyExpirationListener(RedisMessageListenerContainer listenerContainer) {
        super(listenerContainer);
    }

    /**
     * 针对redis数据失效事件，进行数据处理
     * @param message
     * @param pattern
     */
    @Override
    public void onMessage(Message message, byte[] pattern) {
        // 用户做自己的业务处理即可,注意message.toString()可以获取失效的key
        String expiredKey = message.toString();
        String keyPrefix = CommonConstant.SXAQ_NEXT_UPLOAD_TAG+"_";

        String[] str = expiredKey.split("/");
        String mobile_model = "";
        String mobile_system = "";
        String mobile_version = "";
        String mobile_single = "";
        if (str.length == 5) {
            mobile_model = str[1];
            mobile_system = str[2];
            mobile_version = str[3];
            mobile_single = str[4];
        }

        // 如果包含定义的key 就需要进行一系列逻辑操作
        if (expiredKey.contains(keyPrefix)){
            try {
                // 不确定属否会删除对应的key 如果有的话 这里先强制删除
                if (redisUtil.hasKey(expiredKey)) {
                    redisUtil.del(expiredKey);
                }
                // 取出userId
                String[] strings = expiredKey.split(keyPrefix);
                if (strings.length==2) {
                    String userId = strings[1].substring(0, strings[1].indexOf("/"));
                   // 查询用户信息
                    SysUser sysUser = sysUserService.getById(userId);
                    if (sysUser!=null) {
                        Integer eventType = sysUser.getEventType();
                        // 如果是在场内
                        if (eventType==1) {
                            // 重启下一个循环
                            redisUtil.set(expiredKey, userId, Long.parseLong(redisUploadTime));
                            // 添加检测异常(服务器)记录
                            RecordAbnormal recordAbnormal = new RecordAbnormal();
                            recordAbnormal.setPhone(sysUser.getPhone());
                            recordAbnormal.setUserId(sysUser.getId());
                            recordAbnormal.setUsername(sysUser.getUsername());
                            recordAbnormal.setRealname(sysUser.getRealname());
                            recordAbnormal.setType(5);
                            recordAbnormal.setWorkNo(sysUser.getWorkNo());
                            recordAbnormal.setMobileModel(mobile_model);
                            recordAbnormal.setMobileSystem(mobile_system);
                            recordAbnormal.setMobileVersion(mobile_version);
                            recordAbnormal.setMobileSingle(mobile_single);
                            recordAbnormalService.save(recordAbnormal);
                        }
                    }
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }
}
